package com.woniu.woniu_bx.config;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import com.woniu.woniu_bx.filter.UserLoginFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Properties;

/**
 * @author : 王泰元
 * @className: GlobalConfig
 * @description: TODO
 * @date : 2022/8/29 10:59
 */
@Configuration
public class GlobalConfig {


    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("dialect", "Mysql");
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }


    /**
     * 跨域过滤器
     * @author: 王泰元
     * @return 跨域过滤器的注册 bean
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        // 允许所有域名进行跨域调用
        config.addAllowedOriginPattern("*");

        // 允许跨越发送cookie
        config.setAllowCredentials(true);

        // 放行全部原始头信息
        config.addAllowedHeader("*");

        // 允许所有请求方法跨域调用
        config.addAllowedMethod("*");

        // 暴露响应头
        config.addExposedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));

        // 设置过滤器执行顺序，最前
        bean.setOrder(Integer.MIN_VALUE);
        return bean;
    }

    /**
     * spring-security密码加密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private UserDetailsService userDetailsService;
    /**
     * 自定义DaoAuthenticationProvider，解决serNotFoundException被隐藏
     * @return
     */
    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider(){
        DaoAuthenticationProvider daoAuthenticationProvider=new DaoAuthenticationProvider();
        daoAuthenticationProvider.setHideUserNotFoundExceptions(false);//设置为false，UserNotFoundException可生效
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;
    }
    // 将前台登录过滤器注册到 spring 过滤器链中
    @Bean
    public FilterRegistrationBean<UserLoginFilter> userLoginFilterBean() {
        FilterRegistrationBean<UserLoginFilter> userLoginFilterBean = new FilterRegistrationBean<>();
        userLoginFilterBean.setFilter(this.userLoginFilter());

        userLoginFilterBean.setOrder(Integer.MIN_VALUE + 1);
        return userLoginFilterBean;
    }

    // 前台登录过滤器
    @Bean
    public UserLoginFilter userLoginFilter() {
        return new UserLoginFilter();
    }
}
